<?php

App::uses('AppController', 'Controller');

/**
 * Facturaciones Controller
 *
 * @property Facturacione $Facturacione
 * @property PaginatorComponent $Paginator
 */
class FacturacionesController extends AppController
{

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');
    public $layout = 'recargas';

    public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow();
    }
    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->Facturacione->recursive = 0;
        $this->set('facturaciones', $this->Paginator->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        if (!$this->Facturacione->exists($id))
        {
            throw new NotFoundException(__('Invalid facturacione'));
        }
        $options = array('conditions' => array('Facturacione.' . $this->Facturacione->primaryKey => $id));
        $this->set('facturacione', $this->Facturacione->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        if ($this->request->is('post'))
        {
            $this->Facturacione->create();
            if ($this->Facturacione->save($this->request->data))
            {
                $this->Session->setFlash(__('The facturacione has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The facturacione could not be saved. Please, try again.'));
            }
        }
        $pedidos = $this->Facturacione->Pedido->find('list');
        $users = $this->Facturacione->User->find('list');
        $this->set(compact('pedidos', 'users'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        if (!$this->Facturacione->exists($id))
        {
            throw new NotFoundException(__('Invalid facturacione'));
        }
        if ($this->request->is(array('post', 'put')))
        {
            if ($this->Facturacione->save($this->request->data))
            {
                $this->Session->setFlash(__('The facturacione has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The facturacione could not be saved. Please, try again.'));
            }
        } else
        {
            $options = array('conditions' => array('Facturacione.' . $this->Facturacione->primaryKey => $id));
            $this->request->data = $this->Facturacione->find('first', $options);
        }
        $pedidos = $this->Facturacione->Pedido->find('list');
        $users = $this->Facturacione->User->find('list');
        $this->set(compact('pedidos', 'users'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        $this->Facturacione->id = $id;
        if (!$this->Facturacione->exists())
        {
            throw new NotFoundException(__('Invalid facturacione'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Facturacione->delete())
        {
            $this->Session->setFlash(__('The facturacione has been deleted.'));
        } else
        {
            $this->Session->setFlash(__('The facturacione could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }
    
    public function listado()
    {
        
        if(!empty($this->request->data))
        {
            $fecha_ini = $this->request->data['Facturacione']['fecha_ini'];
            $fecha_fin = $this->request->data['Facturacione']['fecha_fin'];
            if($fecha_ini != null && $fecha_fin != null)
            {
                $condiciones['Facturacione.fecha_transaccion >='] = $fecha_ini;
                $condiciones['Facturacione.fecha_transaccion <='] = $fecha_fin;
            }
            $factura = $this->request->data['Facturacione']['factura'];
            if($factura != null)
            {
                $condiciones['Facturacione.numero_factura'] = $factura;
            }
            //debug($condiciones);exit;
            $transacciones = $this->Facturacione->find('all', array(
                'recursive'=>-1, 
                'conditions' => $condiciones,           
            ));
        }
        else{
            $transacciones = $this->Facturacione->find('all', array(
                'recursive'=>-1,            
            ));
        }
        
        $this->set(compact('transacciones','fecha_ini','fecha_fin'));      
    }
    
    public function anulacion($idFactura = null)
    {
        $data = array('id'=>$idFactura, 'estado'=>0);
        if($this->Facturacione->save($data)){
            $this->redirect(array('controller'=>'Facturaciones', 'action'=>'listado'));
        }
    }

}
